package com.lanyou.cook.util.curator;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.apache.curator.framework.recipes.queue.QueueSerializer;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class JsonElementSerializer implements QueueSerializer<JsonElement> {

	private final Charset charset;

	public JsonElementSerializer(Charset charset) {
		this.charset = charset;
	}

	@Override
	public byte[] serialize(JsonElement item) {
		return item.toString().getBytes(charset);
	}

	@Override
	public JsonElement deserialize(byte[] bytes) {
		return new JsonParser().parse(new InputStreamReader(new ByteArrayInputStream(bytes), charset));
	}
}
